import { defineStore } from "pinia";
import {ref} from 'vue'
import { messageListService } from '@/api/message'
import {  getRoleByUserNameService, userInfoService } from "@/api/user";
const useUserInfoStore = defineStore('userInfo',()=>{
    //定义状态相关的内容
    const info = ref({})//基本信息
    const unreadMessageNum = ref()//未读消息数
    const role = ref()//权限
    //修改状态的值
    const setInfo = (newInfo) => {
        info.value = newInfo;
    }
    //清除状态的值
    const removeInfo = () => {
        info.value = {}
        unreadMessageNum.value = {}
    }
    //其他方法
    const countUnread = async () => {
        let params = {
            pageNum: 0,
            pageSize: 0,
            state: "未读"
        }
        let result = await messageListService(params);
        unreadMessageNum.value = result.data.total;//渲染到所有的视图上
    }
    const renewInfo = async() => {//重新从数据库获取个人信息
        const result = await userInfoService();
        info.value = result.data;
        const result2 = await getRoleByUserNameService(result.data.username);
        role.value = result2.data;
    }
    return {info,unreadMessageNum,role,setInfo,removeInfo,countUnread,renewInfo}
},{persist:true})

export default useUserInfoStore;